import { MaterialApi, modifyMaterialInfo } from '@/api/premium/content/material'
import { ModifyMaterialInfoReq } from '@/api/premium/content/model/materialModel'
import {
  MaterialItem,
  MaterialStatus,
} from '@/components/ItemList/MaterialList/types'
import { useStore } from '@/store'
import { message } from 'ant-design-vue'
import { computed } from 'vue'

const useUpdateShelve = (searchApi: () => Promise<void>) => {
  const store = useStore()
  /**
   * 下架素材
   */
  const unShelveMaterial = async (item: MaterialItem) => {
    const reqData: ModifyMaterialInfoReq = {
      id: item.id,
      status: MaterialStatus.FORBIDDEN,
    }
    await modifyMaterialInfo(reqData)
    message.success('下架成功')
    await searchApi()
  }
  /**
   * 上架素材
   */
  const shelveMaterial = async (item: MaterialItem) => {
    const reqData: ModifyMaterialInfoReq = {
      id: item.id,
      status: MaterialStatus.NORMAL,
    }
    await modifyMaterialInfo(reqData)
    message.success('上架成功')
    await searchApi()
  }
  const modifyMaterialInfoLoading = computed(() =>
    store.getters['global/isOpLoading'](MaterialApi.UPDATE_MATERIAL_INFO)
  )

  return { unShelveMaterial, shelveMaterial, modifyMaterialInfoLoading }
}

export default useUpdateShelve
